package clientLogin;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		response.setContentType("text/html");
		
		String login = request.getParameter("login");
		String pwd = request.getParameter("pwd");
		JDBCClientDAO DAO = new JDBCClientDAO();
		
		if (DAO.searchByLogin(login) == true) {
		
			String password = DAO.getByLogin(login).getPwd();
			if (pwd.equals(password))
			{
				int Client_ID = DAO.getByLogin(login).getId();
				session.setAttribute("clientID", Client_ID);
				String nextJSP = "/homePage.jsp";
				RequestDispatcher dispatcher = getServletContext()
						.getRequestDispatcher(nextJSP);
				dispatcher.forward(request, response);
				return;
			} else {
				String nextHTML = "/WrongPWD.html";
				RequestDispatcher dispatcher = getServletContext()
						.getRequestDispatcher(nextHTML);
				dispatcher.forward(request, response);
				return;
			}
		} else {
			String nextHTML = "/WrongLogin.html";
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(nextHTML);
			dispatcher.forward(request, response);
			return;
		}
	}

}
